package cn.initcap.designpattern.pattern.creational.simplefactory;

/**
 * 简单工厂方法不属于GOF23设计模式中的；
 *
 * @author initcap
 * @date 2018/8/28 AM9:08
 */
public class VideoFactory {

    public static final String JAVA = "java";
    public static final String PYTHON = "python";

    /**
     * 使用反射优化简单工厂方法；
     *
     * @param c 类对象
     * @return BaseVideo
     */
    public BaseVideo getVideo(Class c) {
        BaseVideo baseVideo = null;
        try {
            baseVideo = (BaseVideo) Class.forName(c.getName()).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return baseVideo;
    }

    public BaseVideo getVideo(String type) {
        if (JAVA.equalsIgnoreCase(type)) {
            return new JavaBaseVideo();
        } else if (PYTHON.equalsIgnoreCase(type)) {
            return new PythonBaseVideo();
        }
        return null;
    }

}
